package com.wt.admin.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.wt.admin.entity.LoginAccount;
import com.wt.admin.entity.WqunAccount;
import com.wt.admin.entity.WqunBlog;
import com.wt.admin.entity.WqunProject;
import com.wt.admin.service.WqunAdminService;
import com.wt.admin.service.WqunBlogService;
import com.wt.admin.service.WqunProjectService;
import com.wt.commonutils.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@Api(tags = "数据请求")
@RestController
@RequestMapping("/wqunDataStatisticsController")
@CrossOrigin
public class WqunDataStatisticsController {


    @Autowired
    private WqunBlogService wqunBlogService;
    @Autowired
    private WqunProjectService wqunProjectService;
    @Autowired
    private WqunAdminService wqunAdminService;


    LocalDateTime currentDate = LocalDateTime.now();
    SimpleDateFormat dft = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
    int year = currentDate.getYear();
    int nowMonth = currentDate.getMonthValue();
    Date date = new Date();

    //近十年内的数据
    @ApiOperation("近十年内的数据")
    @GetMapping("tenYearData")
//    @PreAuthorize("hasAnyAuthority('wqunDataStatisticsController:tenYearData')")
    public R tenYearData(){
        //统计近10年的数据
        Map<String,int[][]>tenYearData = new HashMap<>();
        tenYearData.put("dataCode",dataCode());
        tenYearData.put("dataArticle",dataArticle());
        tenYearData.put("dataProject",dataProject());
        tenYearData.put("dataNewUser",dataNewUser());
        tenYearData.put("dataAllUser",dataAllUser());


        String[] yearData = new String[10];
        //查询的十年年份
        for (int i = 0; i < 10; i++) {
            yearData[i] = (year-9 + i) + "-01-01";
        }
        return R.ok().data("tenYearData",tenYearData).data("yearData",yearData);
    }

    //近10年每个月的新增的人数
    public int[][] dataNewUser(){
        int [][] arr = new int[10][12];
        QueryWrapper<WqunAccount> wqunBlogQueryWrapper = new QueryWrapper<>();
        for (int i = 9; i >= 0; i--) {
            //月份获取
            for (int i1 = 1; i1 <= 12; i1++) {
                wqunBlogQueryWrapper  = new QueryWrapper<>();
                String month = i1<10? "0" + i1:i1 + "";
                String start = (year-i) + "-" + month + "-01 00:00:00";
                try {
                    date = dft.parse(start);
                } catch (ParseException e) {
                    throw new RuntimeException(e);
                }
                Calendar calendar = Calendar.getInstance();
                calendar.setTime(date);
                // calendar.add(Calendar.MONTH, -1);
                calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMaximum(Calendar.DAY_OF_MONTH));
                String end =  dft.format(calendar.getTime());
                wqunBlogQueryWrapper.ge("created_time",start);
                wqunBlogQueryWrapper.le("created_time",end);
                int count = wqunAdminService.count(wqunBlogQueryWrapper);
                arr[9-i][i1-1] = count;
            }
        }
        return arr;
    }

    //近10年每个月的总人数
    public int[][] dataAllUser(){
//        System.out.println("nowMonth = " + nowMonth);
        int [][] arr = new int[10][12];
        QueryWrapper<WqunAccount> wqunBlogQueryWrapper = new QueryWrapper<>();
        for (int i = 9; i >= 0; i--) {
            //月份获取
            for (int i1 = 1; i1 <= 12; i1++) {
                wqunBlogQueryWrapper  = new QueryWrapper<>();
                String month = i1<10? "0" + i1:i1 + "";
                Calendar calendar = Calendar.getInstance();


                String start = (year-i) + "-" + month + "-01 00:00:00";
                try {
                    date = dft.parse(start);
                } catch (ParseException e) {
                    throw new RuntimeException(e);
                }

                if(i == 0 && nowMonth < i1){
                    arr[i][i1-1] = 0;
                    break;
                }

                calendar.setTime(date);
                // calendar.add(Calendar.MONTH, -1);
                calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMaximum(Calendar.DAY_OF_MONTH));
                String end =  dft.format(calendar.getTime());
                wqunBlogQueryWrapper.le("created_time",end);
                int count = wqunAdminService.count(wqunBlogQueryWrapper);
                arr[9-i][i1-1] = count;
            }
        }
        return arr;
    }



    //近10年源码数据
    public int[][] dataProject(){
        int [][] arr = new int[10][12];
        QueryWrapper<WqunProject> wqunBlogQueryWrapper = new QueryWrapper<>();
        for (int i = 9; i >= 0; i--) {
            //月份获取
            for (int i1 = 1; i1 <= 12; i1++) {
                wqunBlogQueryWrapper  = new QueryWrapper<>();
                String month = i1<10? "0" + i1:i1 + "";
                String start = (year-i) + "-" + month + "-01 00:00:00";
                try {
                    date = dft.parse(start);
                } catch (ParseException e) {
                    throw new RuntimeException(e);
                }
                Calendar calendar = Calendar.getInstance();
                calendar.setTime(date);
                // calendar.add(Calendar.MONTH, -1);
                calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMaximum(Calendar.DAY_OF_MONTH));
                String end =  dft.format(calendar.getTime());
                wqunBlogQueryWrapper.ge("created_time",start);
                wqunBlogQueryWrapper.le("created_time",end);
                int count = wqunProjectService.count(wqunBlogQueryWrapper);
                arr[9-i][i1-1] = count;
            }
        }
        return arr;
    }


    //近10年源码数据
    public int[][] dataCode(){
        int [][] arr = new int[10][12];
        QueryWrapper<WqunProject> wqunBlogQueryWrapper = new QueryWrapper<>();
        for (int i = 9; i >= 0; i--) {
            //月份获取
            for (int i1 = 1; i1 <= 12; i1++) {
                wqunBlogQueryWrapper  = new QueryWrapper<>();
                String month = i1<10? "0" + i1:i1 + "";
                String start = (year-i) + "-" + month + "-01 00:00:00";
                try {
                    date = dft.parse(start);
                } catch (ParseException e) {
                    throw new RuntimeException(e);
                }
                Calendar calendar = Calendar.getInstance();
                calendar.setTime(date);
                // calendar.add(Calendar.MONTH, -1);
                calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMaximum(Calendar.DAY_OF_MONTH));
                String end =  dft.format(calendar.getTime());
                wqunBlogQueryWrapper.ge("created_time",start);
                wqunBlogQueryWrapper.le("created_time",end);
                int count = wqunProjectService.count(wqunBlogQueryWrapper);
                arr[9-i][i1-1] = count;
            }
        }
        return arr;
    }

    //近10年文章数
    public int[][] dataArticle(){
        int [][] arr = new int[10][12];
        QueryWrapper<WqunBlog> wqunBlogQueryWrapper = new QueryWrapper<>();
        for (int i = 9; i >= 0; i--) {
            //月份获取
            for (int i1 = 1; i1 <= 12; i1++) {
                wqunBlogQueryWrapper  = new QueryWrapper<>();
                String month = i1<10? "0" + i1:i1 + "";
                String start = (year-i) + "-" + month + "-01 00:00:00";
                try {
                    date = dft.parse(start);
                } catch (ParseException e) {
                    throw new RuntimeException(e);
                }
                Calendar calendar = Calendar.getInstance();
                calendar.setTime(date);
                // calendar.add(Calendar.MONTH, -1);
                calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMaximum(Calendar.DAY_OF_MONTH));
                String end =  dft.format(calendar.getTime());
                wqunBlogQueryWrapper.eq("status",1);
                wqunBlogQueryWrapper.ge("created_time",start);
                wqunBlogQueryWrapper.le("created_time",end);
                int count = wqunBlogService.count(wqunBlogQueryWrapper);
                arr[9-i][i1-1] = count;
            }
        }
        return arr;
    }

}
